﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using VIP.Extension;

namespace VIP {

    /// <summary>
    /// 延时类
    /// </summary>
    [Serializable]
    class DelayTaskClass
    {
        public string Id;
        public float RealTime;
        public float TaskTime;
        public UnityAction<object> Callback;
        public UnityAction<float> UpdateCallback;
        public object Data;
    }

    public class VIPDelay : VIPMonoSingleton<VIPDelay>
    {

        /// <summary>
        /// 延时列表
        /// </summary>
        //[SerializeField]
        private List<DelayTaskClass> delayList = new List<DelayTaskClass>();

        /// <summary>
        /// 延时用的
        /// </summary>
        void Update()
        {
            for (int i = 0; i < delayList.Count; i++)
            {
                DelayTaskClass dc = delayList[i];
                if (Time.realtimeSinceStartup < dc.TaskTime)
                {
                    UnityAction<float> updateCallback = dc.UpdateCallback;
                    if (updateCallback.IsNotNull())
                    {
                        updateCallback(Time.realtimeSinceStartup - dc.RealTime);
                    }
                    continue;
                }
                else
                {
                    UnityAction<object> callback = dc.Callback;
                    if (callback.IsNotNull())
                    {
                        callback(dc.Data);
                    }
                    delayList.RemoveAt(i);
                    i--;
                }
            }
        }


        /// <summary>
        /// 添加一个延时任务
        /// </summary>
        /// <param name="delay">延时时长</param>
        /// <param name="callback">完成回调</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public string AddDelay(float delay, UnityAction<object> callback, object data = null)
        {
            return AddDelay(delay, null, callback, data);
        }

        /// <summary>
        /// 添加一个延时任务
        /// </summary>
        /// <param name="delay">延时时长</param>
        /// <param name="everyCallback">实时回调</param>
        /// <param name="callback">完成回调</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public string AddDelay(float delay, UnityAction<float> updateCallback, UnityAction<object> callback, object data = null)
        {
            DelayTaskClass dc = new DelayTaskClass();
            float realTime = Time.realtimeSinceStartup;
            float taskTime = realTime + delay;
            string id = VIPRandomUtil.GetString(5);
            dc.Id = id;
            dc.RealTime = realTime;
            dc.TaskTime = taskTime;
            dc.UpdateCallback = updateCallback;
            dc.Callback = callback;
            dc.Data = data;
            delayList.Add(dc);
            return id;
        }

        /// <summary>
        /// 停止并移除一个延时
        /// </summary>
        /// <param name="id">唯一值</param>
        public void RemoveDelay(string id)
        {
            DelayTaskClass dc = delayList.Find((x) => id.Equals(x.Id));
            if (dc.IsNotNull())
            {
                delayList.Remove(dc);
            }
        }

    }

}
